home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Demo Folder / MagicSquare.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  3.5 KB  |  178 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Blob Manager Demonstration:  Magic Square
  3.  *
  4.  * All rows, columns and diagonals of a square must add up to 15.
  5.  *
  6.  * This is a donorless scenario.  The receptors are created, then glued
  7.  * to themselves initially so that they will each have a glob to drag
  8.  * around.
  9.  *
  10.  * 26 July    1986    Paul DuBois
  11.  */
  12.  
  13. # include    "TransSkel.h"
  14.  
  15. # include    "BlobMgr.h"
  16. # include    "BlobDemo.h"
  17.  
  18.  
  19. # define    vButton        5
  20. # define    vBoard        30    /* offset of top of board */
  21. # define    hGap        2
  22. # define    vGap        2
  23. # define    bSqSize        30    /* board square size */
  24.  
  25.  
  26. static WindowPtr        wind;
  27. static BlobSetHandle    receptors;
  28.  
  29. static Boolean            wait;
  30. static ControlHandle    button;
  31. static short            hMid;
  32. static short            hBoard;
  33.  
  34.  
  35. /*
  36.  * Make receptor blob
  37.  */
  38.     
  39. static void
  40. MakeRBlob (Rect *r)
  41. {
  42. BlobHandle    b;
  43. char        c;
  44. static long    rno = 0L;
  45.  
  46.     b = NewBlob (receptors, true, infiniteGlue, false, ++rno);
  47.     OpenBlob ();
  48.     EraseRect (r);
  49.     FrameRect (r);
  50.     InsetRect (r, 5, 7);
  51.     c = rno + '0';
  52.     TextBox (&c, 1L, r, teJustCenter);
  53.     InsetRect (r, -3, -5);
  54.     InvertRect (r);
  55.     InsetRect (r, -2, -2);
  56.     CloseRectBlob (b, r, r);
  57.     GlueGlob (b, b);
  58. }
  59.  
  60.  
  61. static void
  62. MakeReceptors (void)
  63. {
  64.     receptors =    NewBlobSet ();
  65.     MakeBlobGrid (3, 3, hBoard, vBoard, bSqSize, bSqSize,
  66.                     hGap, vGap, MakeRBlob);
  67. }
  68.  
  69.  
  70. static void
  71. Restart (void)
  72. {
  73.  
  74.     HiliteControl (button, dimHilite);
  75.     ShuffleGlobSet (receptors);
  76.     wait = false;
  77. }
  78.  
  79.  
  80. static Boolean
  81. TestConfig (short pos1, short pos2, short pos3)
  82. {
  83. short    sum;
  84.  
  85.     sum = GetBRefCon (BGlob (GetBlobHandle (receptors, pos1)))
  86.         + GetBRefCon (BGlob (GetBlobHandle (receptors, pos2)))
  87.         + GetBRefCon (BGlob (GetBlobHandle (receptors, pos3)));
  88.     return (sum == 15);
  89. }
  90.  
  91.  
  92. static void
  93. CheckStatus (void)
  94. {
  95.     if (TestConfig (0, 1, 2)        /* top row    */
  96.         && TestConfig (3, 4, 5)        /* middle row */
  97.         && TestConfig (6, 7, 8)        /* bottom row */
  98.         && TestConfig (0, 3, 6)        /* left column */
  99.         && TestConfig (1, 4, 7)        /* middle column */
  100.         && TestConfig (2, 5, 8)        /* right column */
  101.         && TestConfig (0, 4, 8)        /* diagonal */
  102.         && TestConfig (2, 4, 6) )    /* diagonal */
  103.     {
  104.         HiliteControl (button, normalHilite);
  105.         wait = true;
  106.     }
  107. }
  108.  
  109.  
  110. static pascal void
  111. Mouse (Point pt, long t, short mods)
  112. {
  113. BlobHandle        b, d;
  114. ControlHandle    ctl;
  115.  
  116.     if (FindControl    (pt, wind, &ctl))
  117.     {
  118.         if (TrackControl (ctl, pt, nil))
  119.             Restart ();
  120.     }
  121.     else if (!wait)
  122.         BlobClick (pt, t, nil, receptors);
  123.     /*
  124.      * Check status even if user just restarted, since the square may
  125.      * by chance fall into a correct configuration.  In that case,
  126.      * the CheckStatus will again enable the restart button.
  127.      */
  128.     CheckStatus ();
  129. }
  130.  
  131.  
  132. static pascal void
  133. Update (Boolean resized)
  134. {
  135.     DrawControls (wind);
  136.     DrawBlobSet (receptors);
  137. }
  138.  
  139.  
  140. static pascal void
  141. Activate (Boolean active)
  142. {
  143.     if (active)
  144.     {
  145.         SetDragRects (wind);
  146.         SetBCPermissions (false, false, false, true, true);
  147.     }
  148. }
  149.  
  150.  
  151. void
  152. MagicInit (void)
  153. {
  154. Rect    r;
  155.  
  156.     SkelWindow (wind = GetDemoWind (magicWindRes),
  157.                 Mouse,            /* mouse clicks */
  158.                 nil,            /* key clicks */
  159.                 Update,            /* updates */
  160.                 Activate,        /* activate/deactivate events */
  161.                 nil,            /* close window */
  162.                 DoWClobber,        /* dispose of window */
  163.                 nil,            /* idle proc */
  164.                 false);            /* irrelevant, since no idle proc */
  165.  
  166.     hMid = wind->portRect.right / 2;
  167.     hBoard = hMid - (3 * (bSqSize + hGap) - hGap) / 2;
  168.     SetRect (&r, hMid - 40, vButton, hMid + 40, vButton + 20);
  169.     button = NewControl (wind, &r, "\pReset", true, 0, 0, 0,
  170.                                 pushButProc, 0L);
  171.  
  172.     MakeReceptors ();
  173.  
  174.     MakeFrontWind (wind);    /* show window */
  175.     Restart ();                /* scramble it */
  176.     SkelDoUpdates ();        /* process update resulting from scramble */
  177. }
  178.